import json


def read_json_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
    return data


def process_data(data):
    result = {}
    for hap_name, value in data.items():
        subsystem_name_en = value.get('subsystem_name_en', '')
        subsystem_name_in_build = value.get('subsystem_name_in_build', '')

        if subsystem_name_en != subsystem_name_in_build or not subsystem_name_in_build:
            hap = {}
            hap["hap_name"] = hap_name
            hap["path"] = value.get('path')
            hap["subsystem_name_in_build"] = value.get("subsystem_name_in_build")
            if subsystem_name_en not in result:
                sub = {}
                sub["duty"] =  value.get('duty')
                sub["hap_list"] = []
                sub["hap_list"].append(hap)
                result[subsystem_name_en] = sub
            else :
                result[subsystem_name_en]["hap_list"].append(hap)
    return result


def write_json_file(data, output_file_path):
    with open(output_file_path, 'w', encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False, indent=2)


def main(input_file_path, output_file_path):
    data = read_json_file(input_file_path)
    processed_data = process_data(data)
    write_json_file(processed_data, output_file_path)


if __name__ == "__main__":
    input_file_path = 'hapinfo.json'  # 输入文件路径
    output_file_path = 'check.json'  # 输出文件路径
    main(input_file_path, output_file_path)